if(!AV.applicationId)
{
AV.init({
 appId: '3SU3NFnWRerFaQFBIiMsRuta-gzGzoHsz',
 appKey: 'zI1dqx2jIXoshpDKl4X5iXeK'
 // appId: 'rQJs4tIvgHSWk42RAg1RL3rp-gzGzoHsz',
 // appKey: 'WBa7JPv0mzaMR7raBBCTRX8l'
  
});
}
function randomPassword(size)
{
  var seed = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
  'a','b','c','d','e','f','g','h','i','j','k','m','n','p','Q','r','s','t','u','v','w','x','y','z',
  '2','3','4','5','6','7','8','9'
  );//数组
  seedlength = seed.length;//数组长度
  var createPassword = '';
  for (i=0;i<size;i++) {
    j = Math.floor(Math.random()*seedlength);
    createPassword += seed[j];
  }
  return createPassword;
}
function createNew(tstr,cstr,rstr)
{
 // 声明一个 Counter 类型
  var TimeTable = AV.Object.extend('TimeTable');
  // 新建一个 Counter 对象
  var timeTable = new TimeTable();
  var rpass = randomPassword(8);
  timeTable.set('passwd', rpass);
  timeTable.set('teacher', tstr);
  timeTable.set('class', cstr);
  timeTable.set('room', rstr);
  
  timeTable.save().then(function (tt) {
    // 成功保存之后，执行其他逻辑.
    showResponse('新建云记录: ' + rpass);
	var passele=document.getElementById('passwd');
	passele.value=rpass;
  }, function (error) {
    // 异常处理
    console.error('Failed to create new object, with error message: ' + error.message);
  });
}
function setupLists(tstr,cstr,rstr)
{
  var list = document.getElementById("teacher"+"list");
  list.innerHTML="";
  var ts=tstr.split('|');
  for(var i=0;i<ts.length;i++)
      createRow('teacher',ts[i]);
	  
  totalClasses = 0;
  list = document.getElementById("class"+"list");
  list.innerHTML="";
  var cs = cstr.split('|');
  for(var i=0;i<cs.length;i++)
  {
      totalClasses += createRow('class', cs[i]);
	  
  }
  list = document.getElementById("room"+"list");
  list.innerHTML="";
  var rs = rstr.split('|');
  for(var i=0;i<rs.length;i++)
  {
      createRow('room', rs[i]);
  }
}
var pass='';
var totalClasses = 0;
var counts = {};
var efforts = {};
//used by final result table
var  rooms=[];
var  classes=[];
var  nops=[];
var dates=[];

function exportTr(trId)
{
	var trEle = document.getElementById(trId);
	 var titlestr='';
	 for(var i=0;i<trEle.children.length;i++)
	 {
		if(trEle.children[i].children.length > 0)
		   titlestr += trEle.children[i].children[0].innerText;
		else
		    titlestr += trEle.children[i].innerText;
		titlestr += ",";
	 }
	 titlestr += "\n";
	 return titlestr;
}

function trConflict(trId)
{
var trEle = document.getElementById(trId);
let trNames ={};
	 for(let i=0;i<trEle.children.length;i++)
	 {

		if(trEle.children[i].children.length > 0)
		{   
			let name = trEle.children[i].children[0].innerText;
			let nameFound =false;
			for(n in trNames)
			{
			   if(n === name)
			   {   
				  nameFound = true;
			   }
			}
			if(nameFound)
			{
			trNames[trEle.children[i].children[0].innerText] += 2;
			}
			else
			{
			trNames[trEle.children[i].children[0].innerText] = 1;
			}
		}

	 }
	 for(let i=0;i<trEle.children.length;i++)
	 {
	    
		if(trEle.children[i].children.length > 0)
		{   
			let name = trEle.children[i].children[0].innerText;
			if(trNames[name]>1)
			 {
			 $(trEle.children[i].children[0]).addClass('conflict');
			 }
			 else
			 {
			 $(trEle.children[i].children[0]).removeClass('conflict');
			 }
		}

	 }
}
function refreshResult()
{
	for(let i=0;i<classes.length;i++)
	{
	let trid = "class"+(i+1);  
	let trEle = ele(trid);
	
	for(let i=(trEle.children.length-rooms.length),j=1;i<trEle.children.length;i++,j++)
	 {
	    
		if(trEle.children[i].children.length > 0)
		{   
			let name = trEle.children[i].children[0].innerText;
			ele(trid+"-"+j).innerHTML = name;
		}

	 }
	}
}
function conflictDetect(p1,p2)
{
    refreshResult();
	for(var i=0;i<totalClasses;i++)
	{
	trConflict("class"+(i+1));
	}
}

function updateEfforts()
{
    var data={};
    for(var i=0;i<totalClasses;i++)
	 {
	   let classkey = "class"+(i+1);
	   let trele = ele(classkey);
	   for(let j=0;j<trele.children.length;j++)
	   {
	      if(trele.children[j].children.length > 0)
		  {
		     trele.children[j].children[0].effort = trele.children[j].id;
		  }
	   }
	 }
}
function exportResult()
{
     //export title
	
	 var titlestr="data:text/csv;charset=utf-8,\ufeff";
	 titlestr += exportTr("classTitle");
	 console.log(titlestr);
	 for(var i=0;i<totalClasses;i++)
	 {
	   titlestr += exportTr("class"+(i+1));
	 }
	 console.log(titlestr);
	 
	 var encodedUri = encodeURI(titlestr);
	var link = document.createElement("a");
	link.setAttribute("href", encodedUri);
	link.setAttribute("download", "result.csv");

	document.body.appendChild(link);
	link.click();

	 
}
function showResponse(str)
{
document.getElementById('result').innerHTML=str;
}
function loadFromCloud()
{
cleandata();
var passele=document.getElementById('passwd');
pass = passele.value;
  var query = new AV.Query('TimeTable');
  query.equalTo('passwd', pass);
  query.find().then(function (results) {
      if(results.length > 0)
	  {
	  var tstr=results[0].get('teacher');
	  var cstr=results[0].get('class');
	  var rstr=results[0].get('room');
	  var title=results[0].get('title');
	  var classData = results[0].get('classdata');
	  var countsstr = results[0].get('counts');
	  var effortsstr = results[0].get('efforts');
	  setupLists(tstr,cstr,rstr);
	  if(title)
		ele('title_in').value = title;
	  if(classData)
	  {  
		ele('overalltable').innerHTML = classData;
		enableDragDrop();
	  }
	  if(countsstr)
	  {
	      counts = JSON.parse(countsstr);
	  }
	  if(effortsstr)
	  {
	      efforts = JSON.parse(effortsstr);
	  }
	  updateEfforts();
	  createRusultTable();
	  refreshResult();
	  showResponse('云端加载成功');
	  }
	  else
	  {
	  showResponse("未找到记录");
	  }
  }, function (error) {
      showResponse('失败:'+error.message);
	 console.error('Failed to find object, with error message: ' + error.message);
  });
}
function saveToCloud()
{
  var r=confirm("确认覆盖云端的记录？")
  if (r==true)
    {
	var tstr = getStrList('teacher');
	var cstr = getStrList('class');
	var rstr= getStrList('room');

    var title = ele('title_in').value;
	if(tstr || cstr || rstr || title)
	  saveAllStrToCloud(tstr,cstr,rstr, title, ele('overalltable').innerHTML);
    }


}
function getStrList(cate)
{
   var tstr='';
		var editTable=document.getElementById(cate+'list');
		
		for(var i=0;i<editTable.children.length;i++){
		  tstr += editTable.children[i].innerText;
		  if(i<(editTable.children.length-1))
			tstr += '|';
		}
		console.log(tstr);
		return tstr;

}
function saveAllStrToCloud(tstr,cstr,rstr,title,classData)
{
var passele=document.getElementById('passwd');
pass = passele.value;
  var query = new AV.Query('TimeTable');
  query.equalTo('passwd', pass);
  query.find().then(function (results) {
      if(results.length > 0)
	  {
	  if(tstr)
		results[0].set('teacher', tstr);
	  if(cstr)
		results[0].set('class', cstr);
	  if(rstr)
		results[0].set('room', rstr);
	  if(title)
	     results[0].set('title', title);
	  if(classData)
         results[0].set('classdata', classData); 
	  if(counts)
	  {
		results[0].set('counts', JSON.stringify(counts)); 
	  }
	  if(efforts)
	  {
	    results[0].set('efforts', JSON.stringify(efforts)); 
	  }
	  results[0].save().then(function (tt) {});
	  document.getElementById('result').innerHTML='保存成功';
	  }
	  else
	  {
	  createNew(tstr,cstr,rstr);
	  
	  }
  }, function (error) {
      createNew(tstr,cstr,rstr);
	  console.log('Failed to find object, with error message: ' + error.message);
  });

}
function getMinute(timestr)
{
   if(timestr.indexOf(':') == -1)
   {
      return Number(timestr)*60;
   }
   else
   {
      var hms = timestr.split(':');
	  var min = Number(hms[0])*60;
	  min += Number(hms[1]);
	  return min;
   }
}
function createRow(cat,str){

var input = document.getElementById(cat+"input");

var list = document.getElementById(cat+"list");
var li=document.createElement("li");
if(!str)
   str =input.value;
if(str.length <= 0)
   return 0;
var inputxt = str.replace('\uFF1A',':');
if(cat == 'class')
{
   var classtxt = inputxt;
   var strs = classtxt.split(' ');
   li.name=cat+"name";
   if(strs.length < 2)
   {
      alert("格式不对。正确的格式:7月16日 12:00-13:00 语文 （空格不可少，符号必须是英文字符）");
      return 0;
   }
   else
   {

   dates.push(strs[0]);   
   classes.push(strs[1]);
   }
   li.innerHTML=classtxt;
   li.timerange=0;
   li.effortm=0;
   var startm=0;
   var endm =0;
   
}
else if(cat == 'room')
{
   var classtxt = inputxt;
   var strs = classtxt.split(' ');
   li.name=cat+"name";
   
   rooms.push(strs[0]);

   li.innerHTML=classtxt;
}
else
{
li.name=cat+"name";
li.innerHTML=str;
}
li.className='w3-padding-small';
input.value='';
var checkbox=document.createElement("input");
	checkbox.type="checkbox";
	checkbox.name=cat+"checkRow";
li.appendChild(checkbox);
list.appendChild(li);

return 1;
}

function delRow(cat){
	var editTable=document.getElementById(cat+"list");
	if(1){
		var checkboxs=document.getElementsByName(cat+"checkRow");
		for(var i=0;i<checkboxs.length;i++){
			if(checkboxs[i].checked){
				var n=checkboxs[i].parentNode;
				editTable.removeChild(n);
				if(cat == "room")
				{
				  rooms.splice(i,1); 
				}
				else if(cat == "class")
				{
				  classes.splice(i,1);
				}
				i--;
			}
		}
		
	}
}

function listclasses(cat)
{
		var editTable=document.getElementById(cat+"list");
		
		for(var i=0;i<editTable.children.length;i++){
		console.log(editTable.children[i].innerText);
		}

}

function ele(id)
{
  return document.getElementById(id);
}
function cleandata()
{
counts = {};
efforts = {};
classes = [];
rooms = [];
nops = [];
dates = [];
}
function generate()
{
   var assignedItems = document.getElementsByClassName("assigned");
   var r = true;
   if(assignedItems.length > 0)
      r=confirm("所有排课信息将被清除，确定？");
   if (r==true){
    counts = {};
    efforts = {};
	ele("title_h").innerText = ele("title_in").value;
	createTeacherFlist();
	var roomcnt= createRoomFlist();
	createClassFList(roomcnt);

	enableDragDrop();
	
	createRusultTable();
	}
}
function updateInfo()
{
   var assignedItems = document.getElementsByClassName("assigned");
   {
	ele("title_h").innerText = ele("title_in").value;
	updateTeacherList();
	var roomcnt= updateRoomFlist();
	updateClassFList(roomcnt);

	enableDragDrop();
	
	createRusultTable();
    refreshResult();
	}
}

function enableDragDrop()
{

		$(function(){
			$('.left .item').draggable({
				revert:true,
				proxy:'clone'
			});
            $('.right .item').draggable({
				revert:true
			});
			$('.right td.drop').droppable({
				onDragEnter:function(){
					$(this).addClass('over');
				},
				onDragLeave:function(){
					$(this).removeClass('over');
				},
				onDrop:function(e,source){
					$(this).removeClass('over');
					if ($(source).hasClass('assigned')){
                    
                        //switch;
                        var child = this.childNodes[0];
                        if(child && source.parentNode )
                        {
						   var curEffort = child.effort;
						   efforts[child.innerHTML] -= Number(curEffort);
                           child.effort = source.effort;
						   efforts[child.innerHTML] += Number(child.effort);
                           source.parentNode.append(child);
                           source.effort = curEffort;
						   efforts[source.innerHTML] -= Number(child.effort);
						   efforts[source.innerHTML] += Number(source.effort);
						   document.getElementById(child.id + "c").innerHTML = counts[child.innerHTML]+"(" + efforts[child.innerHTML] + ")";
                        }else
						{
						efforts[source.innerHTML] -= Number(source.effort);
						source.effort = this.id;
						efforts[source.innerHTML] += Number(source.effort);
						}
							
						   document.getElementById(source.id + "c").innerHTML = counts[source.innerHTML]+"(" + efforts[source.innerHTML] + ")";

						$(this).append(source);
						
					} else {
					    if(this.children.length > 0)
						{
						  var child = this.children[0];
						    counts[child.innerHTML] -= 1;
							efforts[child.innerHTML] -= Number(child.effort);
							console.log(counts[child.innerHTML]);
							document.getElementById(child.id + "c").innerHTML = counts[child.innerHTML]+"(" + efforts[child.innerHTML] + ")";                    
							child.remove();
						}
						
						
                        if(counts[source.innerHTML])
                            counts[source.innerHTML] += 1;
                        else
                            counts[source.innerHTML] = 1;
                           
                        if(efforts[source.innerHTML])
                           efforts[source.innerHTML] += Number(this.id);
                        else
                           efforts[source.innerHTML] = Number(this.id);
                         
                        console.log
                        console.log(source.id);
                        console.log(this.id);
                        console.log(counts[source.innerHTML]);
                        document.getElementById(source.id + "c").innerHTML = counts[source.innerHTML]+"(" + efforts[source.innerHTML] + ")";
						var c = $(source).clone();
                        c.addClass('assigned');
                        c[0].effort = this.id;
						$(this).empty().append(c);
						c.draggable({
							revert:true
						});
					}
					conflictDetect(this,source);
				}
			});
			$('.left').droppable({
				accept:'.assigned',
				onDragEnter:function(e,source){
					$(source).addClass('trash');
				},
				onDragLeave:function(e,source){
					$(source).removeClass('trash');
				},
				onDrop:function(e,source){
					
                    counts[source.innerHTML] -= 1;
                    efforts[source.innerHTML] -= Number(source.effort);
                    console.log(counts[source.innerHTML]);
                    document.getElementById(source.id + "c").innerHTML = counts[source.innerHTML]+"(" + efforts[source.innerHTML] + ")";                    
                    $(source).remove();
					
					conflictDetect(this,source);
				}
			});
		});
}
function updateRoomFlist()
{
	var classtbl = document.getElementById('classflist');
	if(!classtbl)
	   return;

	var roottr=document.getElementById("classTitle");
	if(roottr)
	{
		var len = roottr.children.length;
		for(var i=len-1;i>=0;i--)
		{
			roottr.removeChild(roottr.children[i]);
		}

	   var td=document.createElement("td");
	   td.className="titled";
	   td.innerText="日期";
	   roottr.appendChild(td);
	   td=document.createElement("td");
	   td.className="title";
	   td.innerText="节次";
	   roottr.appendChild(td);
 
	   roottr.id='classTitle';
		var editTable=document.getElementById("roomlist");

		for(var i=0;i<editTable.children.length;i++)
		{
		td=document.createElement("td");
		td.className="title";
		td.innerHTML=rooms[i];
		roottr.appendChild(td);
		}
	    return editTable.children.length;
	}
}
function createRoomFlist()
{
var classtbl = document.getElementById('classflist');
if(!classtbl)
   return;
var len = classtbl.children.length;
for(var i=len-1;i>=0;i--)
{
	classtbl.removeChild(classtbl.children[i]);
}
var roottr=document.createElement("tr");

if(roottr)
{
   var td=document.createElement("td");
   td.className="titled";
   td.innerText="日期";
   roottr.appendChild(td);
   td=document.createElement("td");
   td.className="title";
   td.innerText="节次";
   roottr.appendChild(td);
 
   roottr.id='classTitle';
	var editTable=document.getElementById("roomlist");

	for(var i=0;i<editTable.children.length;i++)
	{
	td=document.createElement("td");
	td.className="title";
	td.innerHTML=rooms[i];
	roottr.appendChild(td);
	}
	classtbl.appendChild(roottr);
    return editTable.children.length;
}
}

function createRusultTable()
{
    var resultbl = ele("resulttable");
   var editTable=document.getElementById("roomlist");
    var trhtml='<div class="w3-cell-row">';
		    var editClassTable=document.getElementById("classlist");
    let maxcells=2;
	var count =0;
	for(var j=0;j<classes.length;j++)
	{
	    let idstr='class'+(j+1);
		for(var i=0;i<editTable.children.length;i++)
		{
		  if(count>0 && count%maxcells == 0)
		  {
		     trhtml += '</div><br><br><br>';
			 if(count%4 == 0)
			   trhtml += '<div class="w3-border-bottom" style="page-break-after: always;"></div>';
			 trhtml += '<div class="w3-cell-row">';
		  }
		  trhtml += '<div class="w3-padding-large w3-cell"><table class="bottomt_t"><tr><td>科目</td><td>';
		  trhtml += classes[j];
		  trhtml += '</td></tr><tr><td>考场</td><td>';
		  trhtml += rooms[i];
		  trhtml += '</td></tr><tr><td>监考</td><td id="';
		  trhtml += idstr+'-'+(i+1);
		  trhtml += '"></td></tr>';
		  trhtml += '<tr><td>人数</td><td>';
		  trhtml += nops[rooms[i]];
		  trhtml += '</td></tr></table>';
		  trhtml += '</div>'; 
		  count++;
		}
		trhtml += '</div><br>';
	}
	console.log(trhtml);
	resultbl.innerHTML = trhtml;
}
function updateClassFList(roomcnt)
{
  var classtbl = document.getElementById('classflist');
  if(classtbl)
  {
       var dateflags={};
       
	    var editTable=document.getElementById("classlist");
        totalClasses = editTable.children.length;

        for(var i=0;i<editTable.children.length;i++){
        	//clean old class elements;
        	var tr=document.getElementById("class"+(i+1));
        	if(tr)
        	{
        		if(tr.children[0].className == "titled")
        		{
        			tr.removeChild(tr.children[0]);
        		}
        		if(tr.children[0].className == "time")
        		{
        			tr.removeChild(tr.children[0]);
        		}
        	}
        }
		for(var i=0;i<editTable.children.length;i++){
		   
			var tr=document.getElementById("class"+(i+1));
			var td=document.createElement("td");
			
			td.className="time";
			let classstr = editTable.children[i].innerText;
			let classstrs = classstr.split(' ');
			if(classstrs.length == 2)
			{
				td.innerHTML += classstrs[1];

				tr.insertBefore(td, tr.children[0]);
			    if(!dateflags[classstrs[0]])
				{
				  dateflags[classstrs[0]] = true;
				  var cnt =0;
				  for (let k=0;k<dates.length;k++)
				  {
				     if(dates[k] == classstrs[0])
					 {
					 cnt ++;
					 }
				  }
				  let dt = classstrs[0];
				  let digits = dt.match(/\d+/g);
				  let cdate = new Date();
				  let weekdays=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
				  let weekday = "Unknown";
				  if(digits.length == 2)
				  {
                      let curMonth = cdate.getMonth();
                      cdate.setMonth(Number(digits[0])-1);
                      if(curMonth > cdate.getMonth())
                      {
                          //accross a year.
                          cdate.setFullYear(cdate.getFullYear()+1);
                      }
                      cdate.setDate(digits[1]);
                      
                      weekday = weekdays[cdate.getDay()];
				  }
				  let td2=document.createElement("td");
				  td2.className="titled";
				  td2.rowSpan=cnt;
				  td2.innerHTML=dt+"<br>"+weekday;
				  tr.insertBefore(td2, td);
				}				
			}
			
		}
		
		
  }
}
function createClassFList(roomcnt)
{
  var classtbl = document.getElementById('classflist');
  if(classtbl)
  {
       var dateflags={};
	    var editTable=document.getElementById("classlist");
        totalClasses = editTable.children.length;
		for(var i=0;i<editTable.children.length;i++){
		    var tr=document.createElement("tr");
			
			var td=document.createElement("td");
			
			td.className="time";
			let classstr = editTable.children[i].innerText;
			let classstrs = classstr.split(' ');
			if(classstrs.length == 2)
			{
			    if(!dateflags[classstrs[0]])
				{
				  dateflags[classstrs[0]] = true;
				  var cnt =0;
				  for (let k=0;k<dates.length;k++)
				  {
				     if(dates[k] == classstrs[0])
					 {
					 cnt ++;
					 }
				  }
				  let dt = classstrs[0];
				  let digits = dt.match(/\d+/g);
				  let cdate = new Date();
				  let weekdays=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
				  let weekday = "Unknown";
				  if(digits.length == 2)
				  {
                      let curMonth = cdate.getMonth();
                      cdate.setMonth(Number(digits[0])-1);
                      if(curMonth > cdate.getMonth())
                      {
                          //accross a year.
                          cdate.setFullYear(cdate.getFullYear()+1);
                      }
                      cdate.setDate(digits[1]);
                      
                      weekday = weekdays[cdate.getDay()];
				  }
				  let td2=document.createElement("td");
				  td2.className="titled";
				  td2.rowSpan=cnt;
				  td2.innerHTML=dt+"<br>"+weekday;
				  tr.appendChild(td2);
				}
				
				td.innerHTML += classstrs[1];
			}
			else
			{
			td.innerHTML=editTable.children[i].innerText;
			}
			tr.appendChild(td);
			tr.id='class'+(i+1);
			
			for(var j=0;j<roomcnt;j++)
			{
			td=document.createElement("td");
			//if(j == 0)
			//  td.id="60";
			//else
			  td.id=editTable.children[i].effortm;
			td.className='drop';
			tr.appendChild(td);
			}
			classtbl.appendChild(tr);
		}
		
		
  }
}
function updateTeacherList()
{
	var listc = document.getElementById('teacherflist');
	var tlist = document.getElementById('teacherslist');

	var len = 0;
	if(listc)
	   len = listc.children.length;
	for(var i=len-1;i>=0;i--)
	{
	    listc.removeChild(listc.children[i]);
	}
	if(tlist)
		len = tlist.children.length;
	for(var i=len-1;i>=0;i--)
	{
	    tlist.removeChild(tlist.children[i]);
	}
	var editTable=document.getElementById("teacherlist");
	
	for(var i=0;i<editTable.children.length;i++){
	let tname =editTable.children[i].innerText;	
	var tr=document.createElement("tr");
	var td=document.createElement("td");
	
	let html ="<div id='"+(500+i)+"c' class='itemc'>";
	if(counts[tname])
	{
	html += counts[tname]+"(" + efforts[tname] + ")";
	}
	else
	{
		html += "0";
	}
	html += "</div>";

	td.innerHTML = html;
	tr.appendChild(td);
	listc.appendChild(tr);
	tr=document.createElement("tr");
	td=document.createElement("td");
	var div=document.createElement("div");
	div.id=""+(500+i);
	div.className="item";
	td.appendChild(div);
	tr.appendChild(td);
	div.innerHTML = tname;
	tlist.appendChild(tr);
	
	}
}
function createTeacherFlist()
{
var listc = document.getElementById('teacherflist');
var tlist = document.getElementById('teacherslist');

var len = 0;
if(listc)
   len = listc.children.length;
for(var i=len-1;i>=0;i--)
{
    listc.removeChild(listc.children[i]);
}
if(tlist)
	len = tlist.children.length;
for(var i=len-1;i>=0;i--)
{
    tlist.removeChild(tlist.children[i]);
}
		var editTable=document.getElementById("teacherlist");
		
		for(var i=0;i<editTable.children.length;i++){
		
		var tr=document.createElement("tr");
		var td=document.createElement("td");
		td.innerHTML ="<div id='"+(500+i)+"c' class='itemc'>0</div>"
		tr.appendChild(td);
		listc.appendChild(tr);
		tr=document.createElement("tr");
		td=document.createElement("td");
		var div=document.createElement("div");
		div.id=""+(500+i);
		div.className="item";
		td.appendChild(div);
		tr.appendChild(td);
		div.innerHTML = editTable.children[i].innerText;
		tlist.appendChild(tr);
		
		}
}
var showInput = true;
function showInputTable()
{
   if(showInput)
     {
	 ele("inputtable").style.display = "none";
	 showInput = false;
	 }
	 else
	 {
	 ele("inputtable").style.display = "block";
	 showInput = true;
	 }
}
var showResult = true;
function showResultTable()
{
   if(showResult)
     {
	 ele("resulttable").style.display = "none";
	 showResult = false;
	 }
	 else
	 {
	 ele("resulttable").style.display = "block";
	 showResult = true;
	 }
}